#include <osgDB/WriteFile>
#include <osgDB/Registry>

#include <osgODE/Manager>
#include <osgODE/Space>
#include <osgODE/DefaultNearCallback>
#include <osgODE/CollisionParameters>

#include <osg/LightSource>
#include <osg/Light>





using namespace osgODE ;




osg::Group*
create_light(void)
{
    osg::Group* group = NULL ;

    osg::Vec3   position = osg::Vec3(-0.25, -0.5, 1.0) ;
    position.normalize() ;




    osg::LightSource*   ls = new osg::LightSource() ;
    osg::Light* light = new osg::Light() ;

    light->setLightNum(0) ;
    light->setPosition( osg::Vec4(position, 0.0) ) ;
    light->setDirection( -position ) ;
    light->setAmbient( osg::Vec4(0.15, 0.16, 0.17, 1.0) ) ;
    light->setDiffuse( osg::Vec4(1.0, 0.99, 0.98, 1.0) ) ;
    light->setSpecular( osg::Vec4(1.0, 1.0, 1.0, 1.0) ) ;

    ls->setLight(light) ;

    group = ls ;




    return group ;
}





int
main(int argc, char** argv)
{
    {
        osgDB::ReaderWriter::Options* options = new osgDB::ReaderWriter::Options() ;
        options->setOptionString("WriteImageHint=IncludeFile") ;
        osgDB::Registry::instance()->setOptions(options);
    }
    {
        osgDB::ReaderWriter::Options* options = new osgDB::ReaderWriter::Options() ;
        options->setOptionString("Compressor=zlib") ;
        osgDB::Registry::instance()->setOptions(options);
    }



    osg::Group*     root = create_light() ;

    osg::ref_ptr<Manager>   manager = new Manager() ;
    Space*                  space   = new Space() ;
    {

        root->addChild( manager ) ;


        // The space
        manager->setWorld( space ) ;


        // quick setup
        manager->setup( true,       // accept visitors
                        1.0/120.0   // step size
                      ) ;




        osgODE::DefaultNearCallback*    near_cbk = new osgODE::DefaultNearCallback() ;
        osgODE::CollisionParameters*    cp = new osgODE::CollisionParameters() ;

        cp->setMode( dContactApprox1 | dContactFDir1 ) ;
        cp->setMu( 1.0 ) ;

        near_cbk->setCollisionParameters( cp ) ;
        space->setNearCallback( near_cbk ) ;
    }



    osgDB::writeNodeFile(*root, "common_scene.osgb") ;

    return 0 ;
}
